package com.sky.controller.admin;

import com.sky.dto.OrdersCancelDTO;
import com.sky.dto.OrdersDTO;
import com.sky.dto.OrdersPageQueryDTO;
import com.sky.dto.OrdersRejectionDTO;
import com.sky.entity.Orders;
import com.sky.mapper.OrdersMapper;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.OrdersService;
import com.sky.vo.OrderStatisticsVO;
import com.sky.vo.OrderVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController("adminOrderController")
@RequestMapping("/admin/order")
@Slf4j
@Api(tags = "订单相关接口")
public class OrderController {

    @Autowired
    private OrdersService ordersService;

    @ApiOperation("拒单")
    @PutMapping("/rejection")
    public Result rejection(@RequestBody OrdersRejectionDTO ordersRejectionDTO){
        log.info("拒单:{}",ordersRejectionDTO);
        ordersService.rejection(ordersRejectionDTO);
        return Result.success();
    }

    /**
     * 取消订单
     * @param ordersCancelDTO
     * @return
     */
    @ApiOperation("取消订单")
    @PutMapping("/cancel")
    public Result cancel(@RequestBody OrdersCancelDTO ordersCancelDTO){
        log.info("取消订单:{}",ordersCancelDTO);
        ordersService.cancel(ordersCancelDTO);
        return Result.success();
    }

    /**
     * 各个状态的订单数量统计
     * @return
     */
    @ApiOperation("各个状态的订单数量统计")
    @GetMapping("/statistics")
    public Result numOfAllOrdersStatus(){
        OrderStatisticsVO orderStatisticsVO = ordersService.numOfAllOrdersStatus();
        return Result.success(orderStatisticsVO);
    }

    /**
     * 订单搜索
     *
     * @param ordersPageQueryDTO
     * @return
     */
    @ApiOperation("订单搜索")
    @GetMapping("/conditionSearch")
    public Result<PageResult> orderSearch(OrdersPageQueryDTO ordersPageQueryDTO) {
        // 记录日志
        log.info("订单搜索，{}", ordersPageQueryDTO);
        // 根据条件查询订单
        PageResult pageResult = ordersService.pageQuery(ordersPageQueryDTO);

        return Result.success(pageResult);
    }

    /**
     * 查询订单详情
     * @param id
     * @return
     */
    @ApiOperation("查询订单详情")
    @GetMapping("/orderDetail/{id}")
    public Result<OrderVO> getDetailById(@PathVariable Long id) {
        log.info("查询订单详情:");
        OrderVO orderVO = ordersService.getDetailById(id);
        return Result.success(orderVO);
    }

    /**
     * 接单
     * @param ordersDTO
     * @return
     */
    @ApiOperation("接单")
    @PutMapping("confirm")
    public Result confirmById(@RequestBody OrdersDTO ordersDTO ){
        log.info("接单{}",ordersDTO);
        ordersService.confirmById(ordersDTO);
        return Result.success();
    }

    /**
     * 订单派送
     * @param id
     * @return
     */
    @ApiOperation("订单派送")
    @PutMapping("delivery/{id}")
    public Result deliveryById(@PathVariable Long id ){
        log.info("订单派送{}",id);
        ordersService.deliveryById(id);
        return Result.success();
    }

    /**
     * 完成订单
     * @param id
     * @return
     */
    @ApiOperation("完成订单")
    @PutMapping("/complete/{id}")
    public Result completeById(@PathVariable Long id){
        log.info("完成订单:{}",id);
        this.ordersService.completeById(id);
        return Result.success();
    }

    /**
     * 管理端查询订单详情
     * @param id
     * @return
     */
    @ApiOperation("查询订单详情")
    @GetMapping("/details/{id}")
    public Result<OrderVO> getDetailsById(@PathVariable Long id) {
        log.info("查询订单详情:");
        OrderVO orderVO = ordersService.getDetailById(id);
        return Result.success(orderVO);
    }
}
